package com.xingfly.web;

import com.xingfly.model.Comment;
import com.xingfly.model.Event;
import com.xingfly.model.Notification;
import com.xingfly.model.User;
import com.xingfly.security.jwt.annotation.CurrentUser;
import com.xingfly.service.CommentService;
import com.xingfly.service.EventService;
import com.xingfly.service.NotificationService;
import com.xingfly.util.Type;
import com.xingfly.web.model.ResponseModel;
import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

/**
 * Created by SuperS on 04/01/2017.
 */
@RestController
@RequestMapping("comments")
public class CommentController {
    @Autowired
    private CommentService commentService;
    @Autowired
    private NotificationService notificationService;
    @Autowired
    private EventService eventService;

    @GetMapping("{id}")
    public ResponseModel<?> getComment(@PathVariable("id") Long id) {
        Comment comment = commentService.find(id);
        if (comment == null) {
            return ResponseModel.error(HttpStatus.BAD_REQUEST, "comment不存在!");
        }
        return ResponseModel.ok(comment);
    }

    @PostMapping
    @Transactional
    public ResponseModel<?> createComment(@RequestBody Comment comment, HttpServletRequest request, @CurrentUser User me) {
        String objectTypeStr = request.getHeader("objectType");
        String objectIdStr = request.getHeader("objectId");
        Integer objectType = null;
        Long objectId = null;
        try {
            objectType = Integer.parseInt(objectTypeStr);
            objectId = Long.parseLong(objectIdStr);
        } catch (NumberFormatException e) {
            return ResponseModel.error(HttpStatus.BAD_REQUEST, "参数格式错误!");
        }
        comment.setAuthor(me);
        comment.setCommentObjectId(objectId);
        comment.setCommentObjectType(objectType);
        Comment saveComment = commentService.create(comment);
        Event event = eventService.getEvent(objectType, objectId);
        if (event == null) {
            return ResponseModel.error(HttpStatus.BAD_REQUEST, "不存在该对象,无法评论!");
        }
        Notification notification = new Notification(Type.NOTIFY_TYPE_COMMENT,
                saveComment.getId(),
                event, event.getUser(), me, Type.UNREAD);

        //如果有有父级评论id
        if (comment.getCommentParentId() != null) {
            if (commentService.find(comment.getCommentParentId()) != null) {
                //如果有是回复父级评论设置提醒为 评论回复
                notification.setNotificationType(Type.NOTIFY_TYPE_COMMENT_REPLY);
                //被提醒者改为 comment的作者
                notification.setNotifiedUser(commentService.find(comment.getCommentParentId()).getAuthor());
            } else {
                return ResponseModel.error(HttpStatus.BAD_REQUEST, "没有该评论!不能回复!");
            }
        }
        notificationService.doNotify(notification);
        return ResponseModel.ok(saveComment);
    }

    /**
     * 根据类型和id获取所有评论
     */
    @GetMapping("/{type}/{id}")
    public ResponseModel<?> getComments(@PathVariable("type") String type, @PathVariable("id") Long id) {
        return ResponseModel.ok(commentService.getComments(Type.toObjectTypeInt(type.toUpperCase()), id));
    }


}
